<template>
  <div class="prodetail">
    <van-nav-bar fixed title="商品详情页" left-arrow @click-left="$router.back()" />

    <van-swipe class="my-swipe" :autoplay="3000" @change="onChange">
      <van-swipe-item v-for="(image, index) in images" :key="index">
        <img :src="image.external_url" />
      </van-swipe-item>
      <template #indicator>
        <div class="custom-indicator">{{ current + 1 }} / {{ images.length }}</div>
      </template>
    </van-swipe>

    <!-- 商品说明 -->
    <div class="info">
      <div class="title">
        <div class="price">
          <span class="now">￥{{ detail.goods_price_min }}</span>
          <span class="oldprice">￥{{ detail.line_price_min }}</span>
        </div>
        <div class="sellcount">已售{{ detail.goods_sales }}</div>
      </div>
      <div class="msg text-ellipsis-2">
        {{ detail.goods_name }}
      </div>

      <!-- 商品保障 -->
      <div class="service">
        <div class="left-words">
          <span v-for="item in serviceList" :key="item.service_id"><van-icon name="passed" />{{ item.name }}</span>
        </div>
        <div class="right-icon" @click="show = !show">
          <van-icon name="arrow" />
        </div>
      </div>
    </div>

    <!-- 商品评价 -->
    <div class="comment">
      <div class="comment-title">
        <div class="left">商品评价 ({{totalComment}}条)</div>
        <div class="right">查看更多 <van-icon name="arrow" /> </div>
      </div>
      <div class="comment-list">
        <div class="comment-item" v-for="item in commentList" :key="item.comment_id">
          <div class="top">
            <img :src="item.user.avatar_url || defaultImg" alt="">
            <div class="name">{{ item.user.nick_name }}</div>
            <van-rate :size="16" :value="item.score/10" color="#ffd21e" void-icon="star" void-color="#eee"/>
          </div>
          <div class="text-ellipsis-2">
            {{ item.content }}
          </div>
          <div class="time">
            {{ item.create_time }}
          </div>
        </div>
      </div>
    </div>

    <!-- 商品描述 -->
    <div class="desc" v-html="detail.content"></div>

    <!-- 底部 -->
    <div class="footer">
      <div class="icon-home" @click="$router.push('/home')">
        <van-icon name="wap-home-o" />
        <span>首页</span>
      </div>
      <div class="icon-cart" @click="$router.push('/cart')">
        <span v-if="totalPro > 0" class="num">{{ totalPro }}</span>
        <van-icon name="shopping-cart-o" />
        <span>购物车</span>
      </div>
      <div class="btn-add" @click="addCart">加入购物车</div>
      <div class="btn-buy" @click="buyNow">立刻购买</div>
    </div>

    <!-- 点击时显示的控制面板 -->
    <van-action-sheet class="my-action-sheet" v-model="show" title="服务">
      <div class="content">
        <div class="left-words" v-for="item in serviceList" :key="item.service_id">
          <p><van-icon name="passed" />{{ item.name }}</p>
          <p class="p_2">{{ item.summary}}</p>
        </div>
      </div>
    </van-action-sheet>

    <!-- 点击加入购物车 或 立即购买 显示的弹层-->
    <van-action-sheet class="my-action-sheet2" v-model="showPannel" :title="mode === 'cart' ? '加入购物车' : '立刻购买'">
      <div>
        <div class="top">
          <img :src="detail.goods_image" class="top-left"/>
          <div class="top-right">
            <p><span>￥</span>{{ detail.goods_price_min }}</p>
            <p>库存{{ detail.stock_total}}</p>
          </div>
        </div>
        <div class="middle">
          <span>数量</span>
          <count-box :value="value" @addClick="value = $event" @subClick="value = $event"></count-box>
        </div>
        <div class="bottom" v-if="detail.stock_total > 0">
          <div class="btn-add" @click="addToCart" v-if="this.mode === 'cart'">加入购物车</div>
          <div class="btn-buy" @click="buyToNow" v-else>立即购买</div>
        </div>
        <div class="bottom" v-else>
          <div class="btn-add btn-none" >商品已售完</div>
        </div>
      </div>
    </van-action-sheet>
  </div>
</template>

<script>
import { addProToCart, getCartTotal } from '@/api/cart'
import { getComments, getCommentsCount } from '@/api/comment'
import { getProduct, getProService } from '@/api/product'
import defaultImg from '@/assets/default-avatar.png'
import CountBox from '@/components/CountBox.vue'
import handleLogin from '@/mixins/login'
export default {
  name: 'ProDetail',
  mixins: [handleLogin],
  components: {
    CountBox
  },
  data () {
    return {
      images: [],
      current: 0,
      detail: '',
      commentList: [],
      totalComment: '',
      defaultImg,
      serviceList: [],
      show: false,
      showPannel: false,
      mode: 'cart',
      value: 1,
      totalPro: ''
    }
  },
  computed: {
    goodsId () {
      return this.$route.params.goodsId
    },
    token () {
      return this.$store.getters.token
    },
    goodsSku () {
      return this.detail.skuList[0]
    },
    goodsSkuId () {
      return this.goodsSku.goods_sku_id
    }
  },
  created () {
    this.getProDetail()
    this.getCommentList()
    this.getTotalComment()
    this.getService()
    this.getTotalPro()
    // console.log(this.defaultImg)
  },
  methods: {
    onChange (index) {
      this.current = index
    },

    // 根据goodsId获取商品详情
    async getProDetail () {
      const { data: { detail } } = await getProduct(this.goodsId)
      this.detail = detail
      this.images = detail.goods_images
      // console.log(detail)
    },

    // 根据goodsId与limit获取评价详情
    async getCommentList () {
      const { data: { list } } = await getComments(this.goodsId, 3)
      this.commentList = list
    },

    // 根据goodsId获取评价总数
    async getTotalComment () {
      const { data: { total: { all } } } = await getCommentsCount(this.goodsId)
      this.totalComment = all
      // console.log(all)
    },

    // 根据goodsId获取商品保障
    async getService () {
      const { data: { list } } = await getProService(this.goodsId)
      this.serviceList = list
      // console.log(list)
    },

    // 获取购物车总数
    async getTotalPro () {
      const { data: { cartTotal } } = await getCartTotal()
      this.totalPro = cartTotal
    },

    // 加入购物车弹出面板
    addCart () {
      this.mode = 'cart'
      this.showPannel = true
    },

    // 立即购买弹出面板
    buyNow () {
      this.mode = 'buyNow'
      this.showPannel = true
    },

    // 选好数字后添加购物车
    async addToCart () {
      if (!this.isLogin()) return
      const res = await addProToCart(this.goodsId, this.value, this.goodsSkuId)
      // console.log(res)
      this.totalPro = res.data.cartTotal
      this.showPannel = false
    },

    // 选好数字后购买
    async buyToNow () {
      if (!this.isLogin()) return
      // console.log('跳转到订单结算')
      this.$router.push(`/pay?mode=buyNow&goodsId=${this.goodsId}&goodsSkuId=${this.goodsSkuId}&goodsNum=${this.value}`)
    }
  }
}
</script>

<style lang="less" scoped>
.prodetail {
  padding-top: 46px;
  ::v-deep .van-icon-arrow-left {
    color: #333;
  }
  .my-action-sheet {
    ::v-deep .van-action-sheet__header {
      font-weight: bold;
    }
    .content {
      height: 160px;
      width: 90%;
      .left-words {
        font-size: 13px;
        margin-bottom: 20px;
        color: black;
        padding-left: 25px;
        .van-icon {
          margin-right: 10px;
          color: #fa2209;
        }
        .p_2 {
          margin-top: 10px;
          padding-left: 25px;
          color: #6d6d6d;
        }
      }
    }
  }
  .my-action-sheet2 {
    div {
      padding: 3px;
      .top {
        height: 90px;
        display: flex;
        box-sizing: content-box;
        .top-left {
          flex: 3;
          height: 90px;
          width: 100px;
          background-repeat: no-repeat;
        }
        .top-right {
          flex: 8;
          padding-left: 10px;
          p:nth-of-type(1) {
            color: #fe5b12;
            font-size: 18px;
            span {
              font-size: 10px;
              margin-right: 2px;
            }
          }
          p:nth-of-type(2) {
            color: #515151;
            font-size: 11px;
          }
        }
      }
      .middle {
        display: flex;
        justify-content: space-between;
        color: #616162;
        font-size: 12px;
      }
      .bottom {
        display: flex;
        justify-content: center;
        .btn-add, .btn-buy {
          height: 36px;
          line-height: 36px;
          width: 96%;
          border-radius: 18px;
          background-color: #ff9900;
          text-align: center;
          color: #fff;
          font-size: 14px;
        }
        .btn-none {
          background: #cccccc;
        }
      }
    }
  }
  img {
    display: block;
    width: 100%;
  }
  .custom-indicator {
    position: absolute;
    right: 10px;
    bottom: 10px;
    padding: 5px 10px;
    font-size: 12px;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 15px;
  }
  .desc {
    width: 100%;
    overflow: scroll;
    ::v-deep img {
      display: block;
      width: 100%!important;
    }
  }
  .info {
    padding: 10px;
  }
  .title {
    display: flex;
    justify-content: space-between;
    .now {
      color: #fa2209;
      font-size: 20px;
    }
    .oldprice {
      color: #959595;
      font-size: 16px;
      text-decoration: line-through;
      margin-left: 5px;
    }
    .sellcount {
      color: #959595;
      font-size: 16px;
      position: relative;
      top: 4px;
    }
  }
  .msg {
    font-size: 16px;
    line-height: 24px;
    margin-top: 5px;
  }
  .service {
    display: flex;
    justify-content: space-between;
    line-height: 40px;
    margin-top: 10px;
    font-size: 16px;
    background-color: #fafafa;
    .left-words {
      span {
        margin-right: 10px;
      }
      .van-icon {
        margin-right: 4px;
        color: #fa2209;
      }
    }
  }

  .comment {
    padding: 10px;
  }
  .comment-title {
    display: flex;
    justify-content: space-between;
    .right {
      color: #959595;
    }
  }

  .comment-item {
    font-size: 16px;
    line-height: 30px;
    .top {
      height: 30px;
      display: flex;
      align-items: center;
      margin-top: 20px;
      img {
        width: 20px;
        height: 20px;
      }
      .name {
        margin: 0 10px;
      }
    }
    .time {
      color: #999;
    }
  }

  .footer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 55px;
    background-color: #fff;
    border-top: 1px solid #ccc;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    .icon-home, .icon-cart {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      padding: 0 6px;
      .num {
        z-index: 999;
        position: absolute;
        top: 3px;
        right: 270px;
        min-width: 16px;
        padding: 0 4px;
        color: #fff;
        text-align: center;
        background-color: #ee0a24;
        border-radius: 50%;
      }
      .van-icon {
        font-size: 24px;
      }
    }
    .btn-add,
    .btn-buy {
      height: 36px;
      line-height: 36px;
      width: 120px;
      border-radius: 18px;
      background-color: #ffa900;
      text-align: center;
      color: #fff;
      font-size: 14px;
    }
    .btn-buy {
      background-color: #fe5630;
    }
  }
}

.tips {
  padding: 10px;
}
</style>
